package com.lj.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     *  通过用户名判断用户是否存在
     * @param username
     * @return 查询结果
     */
    public boolean getUserByUsername(String username){
        String sql = "SELECT username,password FROM user WHERE username = ?";
        Object[] args = {username};
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, args);
        System.out.println(maps);
//        String username1 = (String) maps.get(0).get("username");
//        String password = (String) maps.get(0).get("password");
//        System.out.println(username1);
//        System.out.println(password);
        if(maps.size() == 0){
            return false;
        }
        return true;
    }

    /**
     * 验证用户的密码是否正确
     * @param username
     * @param password
     * @return
     */
    public boolean getUserByUserNameAndPassword(String username, String password){
        String sql = "SELECT username,password FROM user WHERE username = ? AND password = ?";
        Object[] args = {username, password};
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, args);
        System.out.println(maps);

        if(maps.size() == 0){
            return false;
        }
        return true;
    }


    public boolean loginDao(String username, String password){
        String sql = "select user";
        return true;
    }

    public void increaseLoginTimes(String username){
        String sql = "update user set loginTimes = loginTimes + 1 where username = ?";
        Object[] args = {username};
        jdbcTemplate.update(sql,args);
    }

    public void increaseLoginFailedTimes(String username) {
        String sql = "update user set loginFailedTimes = loginFailedTimes + 1 where username = ?";
        Object[] args = {username};
        jdbcTemplate.update(sql,args);
    }


    public void registerDao(String username, String password){
        String sql = "INSERT INTO user(username, password, loginTimes, loginFailedTimes) values (?,?,0,0)";
        Object[] args = {username, password};
        jdbcTemplate.update(sql, args);
    }


    public void findUsersTimes(){
        String sql = "select username,loginTimes,loginFailedTimes from user";
        List<Map<String, Object>> userList = jdbcTemplate.queryForList(sql);

        for(Map<String, Object> map : userList){
            System.out.println(map);
        }


    }



}
